package com.gtja.beoms.service.impl;

import com.gtja.beoms.dto.FileBO;
import com.gtja.beoms.service.IFTPService;
import com.gtja.beoms.service.IFileService;
import com.gtja.beoms.service.IUploadFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

@Service
public class UploadFileServiceImpl implements IUploadFileService {
    private final static Boolean upCDN = false;
    private final static String CREATOR = "qwoms";
    private final static Logger logger = LoggerFactory.getLogger(UploadFileServiceImpl.class);
    //本地文件放置的目录
    @Value("${uploadFiles.external.ftpLocalPath:/}")
    private String localPath;
    //FTP服务器外网域名
    @Value("${uploadFiles.external.httpHost:https://www.gtja.com/f/}")
    private String httpHost;
    @Value("${uploadFiles.cdn.ftpLocalPath:CDN1/}")
    private String cdnLocalPath;
    @Value("${uploadFiles.cdn.cdnHost:https://www.gtja.com/f/CDN1/}")
    private String cdnHttpHost;
    @Autowired
    private IFileService fileService;
    @Autowired
    private IFTPService ftpService;

    /**
     * @param file
     * @return map, 包含err ,则为为异常内容,否则为 fileUrl 地址
     * @Description: MultipartFile 类型文件上传
     * @Author: Li
     */
    @Override
    public Map<String, String> uploadFile(MultipartFile file) {
        // 2.获取文件名
        String fileName = fileService.reFileName(file.getOriginalFilename(), CREATOR);
        Map<String, String> resultMap = null;
        try {
            resultMap = upload(fileName, file.getSize(), file.getInputStream());
        } catch (IOException e) {
            logger.error("file controller input parameter file.getInputStream IOException");
        }
        return resultMap;
    }

    /**
     * @param file
     * @return map, 包含err ,则为为异常内容,否则为 fileUrl 地址
     * @Description: file 文件类型上传
     * @Author: Li
     */
    @Override
    public String uploadFile(File file) {
        Map<String, String> resultMap = new HashMap<>();
        // 获取文件名
        String fileName = fileService.reFileName(file.getName(), CREATOR);
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            resultMap = upload(fileName, file.length(), fileInputStream);
        } catch (FileNotFoundException e) {
            logger.error("file controller input parameter new FileInputStream FileNotFoundException");
        }
        return resultMap.get("fileUrl");
    }


    /**
     * @Description: 文件上传主处理
     * @Author: Li
     * @Param: [fileName, fileSize, inputStream]
     */
    private Map<String, String> upload(String fileName, long fileSize, InputStream inputStream) {
        // 3.文件上传至FTP
        FileBO fileBO = new FileBO();
        fileBO.setFileName(fileName);
        fileBO.setExt3(CREATOR);
        fileBO.setFileType("IMG");
        fileBO.setSize(fileSize);
        fileBO.setHttpHost(httpHost);
        fileBO.setIsExternal(1L);
        fileBO.setBasePath(localPath);

        Map<String, String> map = new HashMap<>();
        fileBO.setInputStream(inputStream);
        String res = ftpService.upload(fileBO);
        if (res != null) {
            map.put("err", res);
            return map;
        }

        // 4.文件上传信息入库,暂未建表
//        fileService.insertUploadInfoRecord(fileBO);

        // 5.控制是否放在CDN文件夹下面一份
        if (upCDN) {
            fileBO.setHttpHost(cdnHttpHost);
            fileBO.setIsExternal(10L);
            fileBO.setBasePath("/" + cdnLocalPath);
            fileBO.setInputStream(inputStream);
            String cdnRes = ftpService.upload(fileBO);
            if (cdnRes != null) {
                map.put("err", cdnRes);
                return map;
            }
//            fileService.insertUploadInfoRecord(fileBO);
        }

        String reDir = fileBO.getHttpPath().contains("/CDN/") ? fileBO.getHttpPath().replace("/CDN/", "") : fileBO.getHttpPath();
        map.put("fileUrl", fileBO.getHttpHost() + reDir + "/" + fileBO.getFileName());
        return map;
    }

}
